package com.medical.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.device.Application;
import com.medical.device.common.PageInfo;
import com.medical.device.common.R;
import com.medical.device.entity.Demo;
import com.medical.device.entity.Test;
import com.medical.device.entity.WeighingScale;
import com.medical.device.service.IWeighingScaleService;
import com.medical.device.service.impl.WeighingScaleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yy
 * @since 2020-02-20
 */
@Api(tags = "体重秤")
@RestController
@RequestMapping("/weighing_scale")
public class WeighingScaleController {

    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    @Autowired
    IWeighingScaleService iWeighingScaleService;

    @ApiOperation(value = "上传体重秤数据", response = WeighingScale.class)
    @PostMapping("/uploadData")
    public ResponseEntity setWeight(@RequestBody WeighingScale weighingScale) {

        try {
            int result = iWeighingScaleService.add(weighingScale);
            if (result == 1) {
                return ResponseEntity.ok(R.success("上传成功"));
            }
            return ResponseEntity.ok(R.success("上传失败"));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity(R.error(), HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @ApiOperation(value = "获取对比数据", response = WeighingScale.class)
    @GetMapping("/getContrastData")
    public ResponseEntity getContrastData(@RequestParam("uid") Integer uid) {

        try {
            QueryWrapper<WeighingScale> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid", uid).orderByDesc("create_time");
            Page<WeighingScale> page = iWeighingScaleService.page(new Page<>(1, 2), queryWrapper);
            return ResponseEntity.ok(R.success(page));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity(R.error(), HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @ApiOperation(value = "获取体重历史数据", response = WeighingScale.class)
    @GetMapping("/getWeightHistory")
    public ResponseEntity getWeightHistory(@RequestParam("uid") String uid, @RequestParam("startTime") long startTime, @RequestParam("endTime") long endTime) {

        try {
            QueryWrapper<WeighingScale> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid", uid).ge("create_time", startTime).lt("create_time", endTime).orderByAsc("create_time");
            List<WeighingScale> page = iWeighingScaleService.list(queryWrapper);
            return ResponseEntity.ok(R.success(page));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity(R.error(), HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

}

